package com.skivingcloud.develop.controller;

import com.mybatisflex.core.paginate.Page;
import com.skivingcloud.admin.logutils.OperateLog;
import com.skivingcloud.admin.utils.CommonConstant;
import com.skivingcloud.common.exceptions.BusinessException;
import com.skivingcloud.common.utils.PageParameter;
import com.skivingcloud.common.utils.ReturnT;
import com.skivingcloud.common.utils.UuidUtil;
import com.skivingcloud.develop.entity.SysBusinessDesigner;
import com.skivingcloud.develop.service.SysBusinessDesignerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * 应用设计记录 控制层。
 *
 * @author skivingcloud
 * @since 2024-01-18
 */
@RestController
@Tag(name = "表单设计记录接口")
@RequestMapping("/sysBusinessDesigner")
public class SysBusinessDesignerController {

    private static final Logger log = LoggerFactory.getLogger(SysBusinessDesignerController.class);

    @Resource
    private SysBusinessDesignerService sysBusinessDesignerService;

    /**
     * 添加应用设计记录。
     *
     * @param sysBusinessDesigner 应用设计记录
     * @return {@code {code:0,message:"",data:true}} 添加成功，{@code {code:500,message:"",data:false}} 添加失败
     */
    @OperateLog(module="应用设计记录", description = "新增应用设计记录")
    @PostMapping("/save")
    @Operation(summary="保存应用设计记录")
    @PreAuthorize("hasAuthority('sys:businessDesigner:save')")
    public ReturnT<Boolean> save(@Validated @RequestBody @Parameter(description="应用设计记录")SysBusinessDesigner sysBusinessDesigner) {
        try{
            sysBusinessDesigner.setId(UuidUtil.get32UUID());
            sysBusinessDesigner.setVersion(0L);
            if (StringUtils.isBlank(sysBusinessDesigner.getResStatus())) {
                sysBusinessDesigner.setResStatus(CommonConstant.COMMON_YES_OR_NO_NO);
            }
            return ReturnT.ok(sysBusinessDesignerService.saveAndCreateTable(sysBusinessDesigner));
        }catch(Exception e){
            log.error("新增应用设计记录失败", e);
            return ReturnT.error("新增应用设计记录失败");
        }
    }

    /**
     * 根据主键删除应用设计记录。
     *
     * @param id 主键
     * @return {@code {code:0,message:"",data:true}} 删除成功，{@code {code:500,message:"",data:false}} 删除失败
     */
    @OperateLog(module="应用设计记录", description = "删除应用设计记录")
    @PostMapping("/remove/{id}")
    @Operation(summary="根据主键应用设计记录")
    @PreAuthorize("hasAuthority('sys:businessDesigner:remove')")
    public ReturnT<Boolean> remove(@PathVariable("id") @Parameter(description="应用设计记录主键")Serializable id) {
        try{
            return ReturnT.ok(sysBusinessDesignerService.removeById(id));
        }catch(Exception e){
            log.error("删除应用设计记录失败", e);
            return ReturnT.error("删除应用设计记录失败");
        }
    }

    /**
     * 根据主键更新应用设计记录。若源记录已发布则生成新记录
     *
     * @param sysBusinessDesigner 应用设计记录
     * @return {@code {code:0,message:"",data:true}} 更新成功，{@code {code:500,message:"",data:false}} 更新失败
     */
    @OperateLog(module="应用设计记录", description = "修改应用设计记录")
    @PostMapping("/update")
    @Operation(summary="根据主键更新应用设计记录")
    @PreAuthorize("hasAuthority('sys:businessDesigner:update')")
    public ReturnT<Boolean> update(@Validated @RequestBody @Parameter(description="应用设计记录主键")SysBusinessDesigner sysBusinessDesigner) {
        try{
            if (StringUtils.isBlank(sysBusinessDesigner.getResStatus())) {
                sysBusinessDesigner.setResStatus(CommonConstant.COMMON_YES_OR_NO_NO);
            }
            return ReturnT.ok(sysBusinessDesignerService.updateVersion(sysBusinessDesigner));
        }catch(Exception e){
            log.error("保存应用设计记录失败", e);
            return ReturnT.error("保存应用设计记录失败！");
        }
    }

    /**
     * 根据应用设计记录主键获取详细信息。
     *
     * @param id 应用设计记录主键
     * @return 应用设计记录详情
     */
    @GetMapping("/getInfo/{id}")
    @Operation(summary="根据主键获取应用设计记录")
    public ReturnT<SysBusinessDesigner> getInfo(@PathVariable("id") String id) {
        return ReturnT.ok(sysBusinessDesignerService.getSysBusinessDesignerHasDetail(id));
    }

    /**
     * 分页查询应用设计记录。
     *
     * @param parameter 分页对象
     * @return 分页对象
     */
    @PostMapping("/page")
    @Operation(summary="分页查询应用设计记录")
    @PreAuthorize("hasAuthority('sys:businessDesigner:page')")
    public ReturnT<Page<SysBusinessDesigner>> page(@RequestBody @Parameter(description="分页信息")PageParameter<SysBusinessDesigner> parameter) {
        Page<SysBusinessDesigner> page = sysBusinessDesignerService.queryByPage(parameter);
        return ReturnT.ok(page);
    }
    
    
    /**
     * 根据编号获取表单信息,不要用这个方法了
     * @param designerCode 表单设计编号
     * @param type 类型：1-新增表单、2-修改表单、3-详情表单
     * @return 表单设计信息
     */
    @GetMapping("/getSysBusinessDesignerByCodeHasJson/{designerCode}/{type}")
    @Operation(summary="根据编号获取表单信息")
    public ReturnT<SysBusinessDesigner> getSysBusinessDesignerByCodeHasJson(@PathVariable("designerCode") String designerCode, @PathVariable("type") String type){
        try{
            return ReturnT.ok(sysBusinessDesignerService.getSysBusinessDesignerByCodeHasJson(designerCode, type));
        }catch(BusinessException e){
           return ReturnT.error(e.getMessage());
        }
    }

    /**
     * 根据编码获取最新版本已发布的表单信息。
     *
     * @param code 表单设计编码
     * @return 表单设计详情
     */
    @GetMapping("/getLatestInfoByCode/{code}")
    @Operation(summary="根据编码获取最新版本已发布的表单信息")
    public ReturnT<SysBusinessDesigner> getLatestInfoByCode(@PathVariable("code") String code) {
        return ReturnT.ok(sysBusinessDesignerService.getLatestInfoByCode(code));
    }
}
